package ch.nostromo.tiffanys.engines;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import ch.nostromo.lib.controller.NosResources;
import ch.nostromo.lib.controller.NosResourcesException;
import ch.nostromo.lib.util.NosTools;

public class EngineResources extends NosResources {


  /** The res directory. */
  private String resDirectory = "resources";

  /** The gui dir. */
  private String engineDir = "engine";

  /** The jar gui dir. */
  private String jarEngineDir = "resources/engine";
  
  public EngineResources(String language, String skin) {
    super(language, skin);
  }
  

  public String loadEngineResource(String fileName) throws NosResourcesException {
    String result = null;

    try {
      result = loadEngineResourceFromFS(fileName);
    } catch (NosResourcesException e) {
      result = loadEngineResourceFromJar(fileName);
    }

    return result;
  }

  private String loadEngineResourceFromFS(String file) throws NosResourcesException {
    String completeEngineDir = this.engineDir + "/";

    File resDir = new File(this.resDirectory);
    File EngineFile = new File(resDir.getAbsolutePath() + "/" + completeEngineDir, file);

    if (!EngineFile.exists()) {
      throw new NosResourcesException("Engine file not found (" + EngineFile.getAbsolutePath() + ")");
    }

    try {
      FileInputStream fis = new FileInputStream(EngineFile);
      return NosTools.readFile(fis);
    } catch (IOException e) {
      throw new NosResourcesException("Engine file load failed", e);
    }

  }

  private String loadEngineResourceFromJar(String fileName) throws NosResourcesException {
    String completeEngineFile = this.jarEngineDir + "/" + fileName;
    InputStream is = getClass().getClassLoader().getResourceAsStream(completeEngineFile);

    if (is == null) {
      throw new NosResourcesException("Engine Resource file not found with classloader (" + completeEngineFile + ")");
    }

    try {
      return NosTools.readFile(is);
    } catch (IOException e) {
      throw new NosResourcesException("Engine file load failed", e);
    }

  }



  /**
   * Returns a gui resouce as URL. Tries first in the filesystem, later within a jar file.
   * 
   * @param file
   *          the file
   * 
   * @return The gui resouces as url
   * 
   * @throws Exception
   *           the exception
   * 
   * @exception Exception
   *              Exception
   */
  public URL getEngineResource(String file) throws NosResourcesException {
    URL retValue = null;
    try {
      retValue = getEngineResourceFromFs(file);
    } catch (Throwable e) {
      try {
        retValue = getEngineResourceFromJar(file);
      } catch (Throwable e2) {
        throw new NosResourcesException("File: " + e.toString() + " Jar: " + e2.toString());
      }
    }
    return retValue;
  }

  /**
   * Returns a gui resouce as URL (ie an image) from a typical applicatino jar file.
   * 
   * @param file
   *          the file
   * 
   * @return The gui resource as url.
   * @throws NosResourcesException
   * 
   * @throws Exception
   *           the exception
   * 
   * @exception Exception
   *              Exception
   */
  private URL getEngineResourceFromJar(String file) throws NosResourcesException {
    String completeGuiFile = this.jarEngineDir + "/" + file;

    URL retVal = getClass().getClassLoader().getResource(completeGuiFile);

    if (retVal == null) {
      throw new NosResourcesException("Engine file not found in Jar (" + completeGuiFile + ")");
    }

    return retVal;
  }

  /**
   * Returns a gui resouce as URL (ie an image) from the file system.
   * 
   * @param file
   *          the file
   * 
   * @return The guiResourceFromFs value
   * @throws MalformedURLException
   * 
   * @throws Exception
   *           the exception
   * 
   * @exception Exception
   *              Exception
   */
  private URL getEngineResourceFromFs(String file) throws NosResourcesException, MalformedURLException {
    String completeGuiDir = this.engineDir + "/";

    File resDir = new File(this.resDirectory);
    File guiFile = new File(resDir.getAbsolutePath() + "/" + completeGuiDir, file);

    if (!guiFile.exists()) {
      throw new NosResourcesException("GUI file not found (" + guiFile.getAbsolutePath() + ")");
    }

    return guiFile.toURI().toURL();
  }

  
}
